package com.example.zzbus.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.zzbus.R;
import com.example.zzbus.base.BaseActivity;
import com.example.zzbus.base.BaseApplication;
import com.example.zzbus.data.UserData;
import com.example.zzbus.db.User;
import com.example.zzbus.db.UserDao;

import java.util.List;

import baidumapsdk.demo.search.RoutePlanDemo;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {

    @Bind(R.id.et_username)
    EditText etUsername;
    @Bind(R.id.et_pwd)
    EditText etPwd;
    @Bind(R.id.btn_login)
    Button btnLogin;
    @Bind(R.id.tv_register)
    TextView tvRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_login, R.id.tv_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                login();
                break;
            case R.id.tv_register:
                startActivity(new Intent(context,RegisterActivity.class));
                break;
        }
    }
    public void login(){
        String username=etUsername.getText().toString();
        if (TextUtils.isEmpty(username)){
            showTost("请输入用户名");
            return;
        }
        String userpwd=etPwd.getText().toString();
        if (TextUtils.isEmpty(userpwd)){
            showTost("请输入密码");
            return;
        }
        List<User> list= BaseApplication.getmDaoSession().getUserDao().queryBuilder()
                .where(UserDao.Properties.Username.eq(username),UserDao.Properties.Userpwd.eq(userpwd)).list();
        if (list!=null&&list.size()>0){
            showTost("登陆成功");
            UserData.setBoolData(true);
            UserData.setSpString(UserData.sp_userName,list.get(0).getUsername());
            UserData.setSpString(UserData.sp_userNick,list.get(0).getUsernick());
            UserData.setSpString(UserData.sp_userPhone,list.get(0).getUserphone());
            startActivity(new Intent(context, RoutePlanDemo.class));
            finish();
            return;
        }else{
            showTost("用户名密码错误");
        }
    }
}
